package com.ineatconseil.yougo.pages.users.password;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;

import com.ineatconseil.yougo.beans.ActionType;
import com.ineatconseil.yougo.exceptions.RestServiceException;
import com.ineatconseil.yougo.pages.AbstractPage;
import com.ineatconseil.yougo.services.rest.RestPath;

public class ChangePassword extends AbstractPage {

	@Property
	private String oldPassword = "";

	@Property
	private String newPassword = "";

	@Property
	private String confirmatedNewPassword = "";

	@Component
	private Form passwordForm;

	@Log
	public Object onSubmitFromPasswordForm() throws RestServiceException {

		// try {
		List<RestPath> paths = new ArrayList<RestPath>();

		String oldPasswordParam = "?oldPassword=" + oldPassword;
		String newPasswordParam = "&newPassword=" + newPassword;
		String confirmPasswordParam = "&confirmedNewPassword=" + confirmatedNewPassword;

		paths.add(new RestPath("users/" + this.getAuthenticator().getLoggedUser().getId() + "/password"
				+ oldPasswordParam + newPasswordParam + confirmPasswordParam, ""));

		this.getRestServiceInvoker().update(paths, null, null);

		this.setActionMessageOk("user", ActionType.PASSWORD_CHANGE);

		// } catch (RestServiceException e) {
		// passwordForm.recordError(getRESTErrorMessage(e.getStatus()));
		// }

		return null;
	}

}
